#include "playscene.h"
#include <QDebug>
#include <QMenuBar>
#include <QPainter>
#include "mypushbutton.h"
#include <QTimer>
#include <QLabel>
#include <QString>
#include "mycoin.h"
#include "dataconfig.h"
#include <QPropertyAnimation>
#include <QSound>


PlayScene::PlayScene(QWidget *parent) : QMainWindow(parent)
{

}

PlayScene::PlayScene(int levelNum)
{
    QString str = QString("进入了第 %1 关").arg(levelNum);
    qDebug() << str;
    this->levelIndex = levelNum;

    //初始化游戏场景
    //设置固定大小
    this->setFixedSize(320,588);
    //设置图标
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
    //设置标题
    this->setWindowTitle("恭迎彪少翻金币");

    //创建菜单栏
    QMenuBar * bar = menuBar();
    this->setMenuBar(bar);

    QMenu * startmenu = bar->addMenu("开始");

    QAction * quitAction = startmenu->addAction("退出");

    //音效
    QSound * flipSound = new QSound(":/res/ConFlipSound.wav",this);
    QSound * winSound = new QSound(":/res/LevelWinSound.wav",this);
    QSound * backSound = new QSound(":/res/BackButtonSound.wav",this);

    connect(quitAction, &QAction::triggered, this, [=](){
        this->close();
    } );

    MyPushButton * backBtn = new MyPushButton(":/res/BackButton.png", ":/res/BackButtonSelected.png");
    //按钮一定要设置父亲是谁，属于谁的按钮
    backBtn->setParent(this);
    backBtn->move( (this->width() - backBtn->width()) / 2, this->height() - backBtn->height() - 30 );

    //点击返回
    //第二个参数是函数指针
    connect(backBtn, &MyPushButton::clicked, this, [=](){
        qDebug() << "点击了娱乐场景的返回按钮";
        backSound->play();
        //告诉主场景，我返回了，主场景监听ChooseLevelScence的返回按钮
        QTimer::singleShot(300,this,[=](){
            emit this->chooseSceneBack();
        });
    });

    //显示关卡图
    QLabel * label = new QLabel;
    label->setParent(this);//在Qt中，一般new出来的东西都要设置parent，使其在对象树上释放
    QFont font;
    font.setFamily("青鸟华光简行草");
    font.setPointSize(25);
    font.setWeight(QFont::Black);
    QString str2 = QString("Level: %1").arg(this->levelIndex);
    label->setFont(font);
    label->setText(str2);

//    label->setText(QString::number(this->levelIndex));
//    label->move(100,500);
//    label->setFixedSize(120,50);
    label->setGeometry(170, 40, 120, 50);

    dataConfig data;

    //初始化每个关卡的二维数组
    for(int i = 0; i < 4; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            this->gameArray[i][j] = data.mData[this->levelIndex][i][j];
        }
    }

    //胜利图片显示
    QLabel * winLabel = new QLabel;
    QPixmap tmpPix;
    tmpPix.load(":/res/LevelCompletedDialogBg.png");
    winLabel->setParent(this);
    winLabel->setPixmap(tmpPix);
    winLabel->setGeometry(0,0,tmpPix.width(),tmpPix.height());
    winLabel->move( (this->width()-tmpPix.width())*0.5, -tmpPix.height());


    //显示金币背景图
    for (int i = 0; i < 4; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            QLabel * label2 = new QLabel;
            label2->setParent(this);
            label2->setGeometry(0,0,50,50);
            label2->setPixmap(QPixmap(":/res/BoardNode(1).png"));
            label2->move(57 + i*50, 200+j*50);

            //创建金币
            QString str;
            (this->gameArray[i][j] == 1) ? str=":/res/Coin0001.png": str = ":/res/Coin0008.png" ;

            MyCoin * coin = new MyCoin(str);
            coinBtn[i][j] = coin;
            coin->setParent(this);
            coin->move(59 + i*50, 204+j*50);

            //给金币属性赋值
            coin->posX = i;
            coin->posY = j;
            coin->flag = this->gameArray[i][j]; //1正面，0反面
//            qDebug() << "打印标志";
//            qDebug() << coin->flag;
            connect(coin,&MyCoin::clicked,this,[=](){
                coin->changeFlag();
                this->gameArray[i][j] = this->gameArray[i][j] == 0 ? 1 : 0 ;

                //音效
                flipSound->play();
                //延时翻转周围金币
                QTimer::singleShot(300, this, [=](){
                    if(coin->posX + 1 <= 3)
                    {
                        coinBtn[coin->posX + 1][coin->posY]->changeFlag();
                        this->gameArray[coin->posX + 1][coin->posY] = this->gameArray[coin->posX + 1][coin->posY] == 0 ? 1 : 0 ;
                    }
                    if(coin->posX - 1 >= 0)
                    {
                        coinBtn[coin->posX - 1][coin->posY]->changeFlag();
                        this->gameArray[coin->posX - 1][coin->posY] = this->gameArray[coin->posX - 1][coin->posY] == 0 ? 1 : 0 ;
                    }
                    if(coin->posY + 1 <= 3)
                    {
                        coinBtn[coin->posX][coin->posY + 1]->changeFlag();
                        this->gameArray[coin->posX][coin->posY + 1] = this->gameArray[coin->posX][coin->posY + 1] == 0 ? 1 : 0 ;
                    }
                    if(coin->posY - 1 >= 0)
                    {
                        coinBtn[coin->posX][coin->posY - 1]->changeFlag();
                        this->gameArray[coin->posX][coin->posY - 1] = this->gameArray[coin->posX][coin->posY - 1] == 0 ? 1 : 0 ;
                    }

                    //方才把这段代码写到了Qtimer外边，出现了bug，才明白，这个是多线程的
                    //判断是否胜利
                    this->isWin = true;
                    for(int i = 0; i < 4; i++)
                    {
                        for(int j = 0; j < 4; j++)
                        {
                            if(coinBtn[i][j]->flag == false)
                            {
                                this->isWin = false;
    //                            break;
                            }
//                            qDebug() << coinBtn[i][j]->flag;
                        }
                    }
                    if (this->isWin == true)
                    {
                        qDebug() << "胜利了";
                        for(int i = 0; i < 4; i++)
                        {
                            for(int j = 0; j < 4; j++)
                            {
                                coinBtn[i][j]->isWin = true;
                            }
                        }
                        //音效
                        winSound->play();
                        //把胜利图片砸下来
                        QPropertyAnimation * animation = new QPropertyAnimation(winLabel,"geometry");
                        animation->setDuration(1000);
                        animation->setStartValue(QRect(winLabel->x(), winLabel->y(), winLabel->width(), winLabel->height() ));
                        animation->setEndValue(QRect(winLabel->x(), winLabel->y()+180, winLabel->width(), winLabel->height() ));
                        animation->setEasingCurve(QEasingCurve::InOutQuad);
                        animation->start();
                    }
//                    qDebug() << "hello"
                });
            });
        }
    }
}

void PlayScene::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    pix.load(":/res/Title.png");
    pix = pix.scaled(pix.width()*0.5, pix.height()*0.5);
    painter.drawPixmap(10,30,pix.width(),pix.height(),pix);


}
